package spring3.controller;

import java.rmi.RemoteException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.UriBuilder;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.core.util.MultivaluedMapImpl;










import spring3.form.AdministradorVO;
import spring3.form.Cliente;
import spring3.form.Form;
import spring3.form.Administrador;

import org.apache.axis2.AxisFault;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;


// TODO: Auto-generated Javadoc
/**
 * The Class CuentaController.
 */
@Controller
@SessionAttributes
public class CuentaController {

	/**
	 * Menu.
	 *
	 * @return the model and view
	 * @throws RemoteException 
	 */
	@RequestMapping("/menu")
	public ModelAndView menu() throws RemoteException {
		return new ModelAndView("administracion", "command", new Administrador());
	}

	/**
	 * Registrar admin.
	 *
	 * @return the model and view
	 * @throws RemoteException 
	 */
	@RequestMapping("/agregarAdmin")
	public ModelAndView registrarAdmin() throws RemoteException {
		return new ModelAndView("registrarAdmin", "command", new Administrador());
	}

	@RequestMapping(value = "/registrarAdmin", method = RequestMethod.POST)
	public ModelAndView agregarAdministrador(HttpSession session,@ModelAttribute("AdministradorVO") @Valid  spring3.form.Administrador administrador){

		ClientConfig config = new DefaultClientConfig();
		Client client = Client.create(config);
		WebResource webResource = client.resource(UriBuilder.fromUri("http://localhost:8083/AdminREST/rest/ServiceAdministracion/registrarAdministrador").build());
		MultivaluedMap<String, String> formData = new MultivaluedMapImpl();
		formData.add("usuario", ""+administrador.getUsuario());
		formData.add("clave", ""+administrador.getPass());
		ClientResponse response = webResource.type(MediaType.APPLICATION_FORM_URLENCODED_TYPE).post(ClientResponse.class, formData);

		return new ModelAndView("mensaje", "message", response.getEntity(String.class));

	}

	/**
	 * Mostrar Administrador
	 *
	 * @return the model and view
	 * @throws RemoteException 
	 */
	@RequestMapping("/mostrarAdmin")
	public ModelAndView mostrarAdmin() throws RemoteException {

		ClientConfig config = new DefaultClientConfig();
		Client client = Client.create(config);
		WebResource webResource = client.resource(UriBuilder.fromUri("http://localhost:8083/AdminREST/rest/ServiceAdministrador/mostrarAdministradores").build());
		ClientResponse response = webResource.type(MediaType.APPLICATION_FORM_URLENCODED_TYPE).get(ClientResponse.class);

		Form form = new Form();
		Gson gson = new Gson();

		AdministradorVO[] administradoresIngreados = (AdministradorVO[])gson.fromJson(response.getEntity(String.class), AdministradorVO[].class);
		form.setConsultas(administradoresIngreados);
		return new ModelAndView("mostrarClientes" , "Form", form);

	}
	
	
	/**
	 * Menu Cliente.
	 *
	 * @return the model and view
	 * @throws RemoteException 
	 */
	@RequestMapping("/menuCliente")
	public ModelAndView menuCliente() throws RemoteException {
		return new ModelAndView("clientes", "command", new Cliente());
	}
	
	
	/**
	 * Mostrar Cliente
	 *
	 * @return the model and view
	 * @throws RemoteException 
	 */
	@RequestMapping("/mostrarCliente")
	public ModelAndView mostrarCliente() throws RemoteException {

		ClientConfig config = new DefaultClientConfig();
		Client client = Client.create(config);
		WebResource webResource = client.resource(UriBuilder.fromUri("http://localhost:8083/AdminREST/rest/ServiceCliente/mostrarClientes").build());
		ClientResponse response = webResource.type(MediaType.APPLICATION_FORM_URLENCODED_TYPE).get(ClientResponse.class);

		Form form = new Form();
		Gson gson = new Gson();

		AdministradorVO[] administradoresIngreados = (AdministradorVO[])gson.fromJson(response.getEntity(String.class), AdministradorVO[].class);
		form.setConsultas(administradoresIngreados);
		return new ModelAndView("show" , "Form", form);

	}
	
	
	
	
	


}